<?php

function SortOrder($a, $b)
{
    if($a['Orders']>$b['Orders'])
        return 1;
    else if($a['Orders']<$b['Orders'])
        return -1;
    else return 0;
}
    
class ArticleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function showAction()
    {
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        
        //Xử lý phím được nhấn
        $pressedID = $this->_request->getPost('pressed-id');
        $articleDtID = $this->_request->getPost('article-dt-id');
        $action = $this->_request->getPost('action');
        $articleID = $this->_request->getParam('id',-1);
        
        $viewMode = $this->_request->getParam('view-mode');
        $categories = CategoryModel::GetCategory();
        
        if($viewMode=='admin')
        {
            switch ($pressedID)
            {
                case 'bSaveText':
                    $content = $this->_request->getPost('mycontent');
                    switch ($action)
                    {
                        case 'edit-text':
                            ArticleModel::UpdateArticleDtContent($articleDtID, $content);
                            break;
                        case 'add-text':
                            ArticleModel::AddArticleDtTextAfter($articleDtID, $content);
                            break;
                        case 'add-text-top':
                            ArticleModel::AddArticleDtTextTop($articleID, $content);
                            break;
                    }
                    break;
                case 'down':
                    ArticleModel::ChangeOrderArticleDt($articleDtID, true);
                    break;
                case 'up':
                    ArticleModel::ChangeOrderArticleDt($articleDtID, false);
                    break;
                case 'delete':
                    ArticleModel::DeleteArticleDt($articleDtID);
                    break;
                case 'save-image':
                    $imageLink = $this->_request->getPost('image-link');
                    $imageDes = $this->_request->getPost('image-des');
                    switch($action)
                    {
                        case 'edit-image':
                            ArticleModel::UpdateArticleDtImage($articleDtID, $imageLink, $imageDes);
                            break;
                        case 'add-image':
                            ArticleModel::AddArticleDtImageAfter($articleDtID, $imageLink, $imageDes);
                            break;
                        case 'add-image-top':
                            ArticleModel::AddArticleDtImageTop($articleID, $imageLink, $imageDes);
                            break;
                    }
                    break;
                case 'b-edit-title':
                    $title = $this->_request->getPost('t-edit-title');
                    ArticleModel::UpdateTitle($articleID, $title);
                case 'b-edit-cat':
                    $category = $this->_request->getPost('s-category');
                    $pos = strpos($category,'|_');
                    $category= substr($category, $pos+4);
                    $category = CategoryModel::GetIDFromName($category);
                    ArticleModel::UpdateArticleCategoryID($articleID, $category);
            }
        }   //END if($viewMode=='admin')
        
        //LẤY TRANG
        //Lấy thông tin article
        
        $article = ArticleModel::GetArticle($articleID);
        
        $articleDts = ArticleModel::GetArticleDt($articleID);   
        usort($articleDts, "SortOrder");
        
        $this->view->minOrderArticleDtID = $articleDts[0]['ID'];
        $this->view->maxOrderArticleDtID = $articleDts[sizeof($articleDts)-1]['ID'];
        $this->view->article = $article;
        $this->view->articleDts = $articleDts;
        $this->view->viewMode = $viewMode;
        $this->view->categories = $categories;
    }

    public function managerAction()
    {
        // action body
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        
        //Thực hiện các action trước đó
    	
        $pressedID = $this->_request->getPost('pressed-id');
        if($pressedID!=""){
            
            $strChecked = $this->_request->getPost('ListCheckboxChecked');
            $IDsChecked = explode(',',$strChecked);
        	if($pressedID == "bDelete"){
                    ArticleModel::Delete($IDsChecked);
        	}
                if($pressedID == "bNew")
                {
                    //TODO : Add đầy đủ thông tin hơn, chính xác hơn
                    $minCategoryID = CategoryModel::GetMinID();
                    $id = ArticleModel::AddArticle('Nhấp đôi để sửa tiêu đề', 1, $minCategoryID);
                    header('Location: show?id='.$id.'&view-mode=admin') ;
                }
        }
        
        $articles = ArticleModel::GetAllArticle();
        $this->view->articles = $articles;
    }

    public function listAction()
    {
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        
        $categoryID = $this->_request->getParam('categoryid',-1);
        $articles = ArticleModel::GetArticleByCategoryID($categoryID);
        
        foreach($articles as $key=>$article)
        {
            $intro = ArticleModel::GetArticleDtIntro($article['ID']);
            $articles[$key]['Intro']=$intro;
        }
        $this->view->articles = $articles;
    }


}







